package com.cupk.mapper;

import com.cupk.pojo.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Select("select student.*,account.* from student,account where account.stu_id=student.stu_id and account.openid = #{openid}")
    public Student findStudentByOpenid(String openid);

    @Select("select * from student where stu_id = #{stuId} and identity_card=#{identityCard}")
    public Student findStudentByIdentity(String stuId, String identityCard);

    @Select("select student.*,account.* from student,account where account.stu_id=student.stu_id and account.stu_id = #{stuId}")
    public Student findStudentByStuId(String stuId);

    @Select("select * from student where id = #{id}")
    public Student findStudentById(Integer id);

    @Insert("insert into account(openid,unionid,stu_id,update_time) values (#{openid},#{unionid},#{stuId},now())")
    public void addAccount(String stuId, String openid, String unionid);

    @Update("update student set phone = #{phone} where stu_id = #{stuId}")
    public void updateMobile(String stuId, String phone);

    @Select("select black_list from student where id = #{userId}")
    Integer judgeBlackList(Integer userId);

    @Select("<script>" + "SELECT * FROM student " + "WHERE 1=1 " + "<if test='query != null and query != \"\"'>" + "AND (" + "name LIKE CONCAT('%', #{query}, '%') OR " + "stu_id LIKE CONCAT('%', #{query}, '%') OR " + "identity_card LIKE CONCAT('%', #{query}, '%') OR " + "grade LIKE CONCAT('%', #{query}, '%') OR " + "college LIKE CONCAT('%', #{query}, '%') OR " + "major LIKE CONCAT('%', #{query}, '%') OR " + "classes LIKE CONCAT('%', #{query}, '%') OR " + "phone LIKE CONCAT('%', #{query}, '%') " + ")" + "</if>" + "</script>")
    List<Student> searchStudents(@Param("query") String query);

    @Update("update student set black_list = #{status} where id = #{stuId}")
    void setBlackListStatus(Integer stuId, Integer status);

    @Select("select openid from account where stu_id = #{stuId}")
    String findOpenIdByStuId(String stuId);

    @Update("update student set avatars = #{url} where stu_id = #{stuId}")
    void updateAvatars(String stuId, String url);
}